#include "Cameras/Camera.h"
#include "Object3D/Object3D.h"
#include "Angles/MathAngles.h"
#include <xnamath.h>

#if defined (_DEBUG)
#include "Memory/MemLeaks.h"
#endif

//-------------------------------------------------
CCamera::CCamera(float _fZNear, float _fZFar, float _fFOV, float _fAspectRatio, CObject3D* _pObject)
	: m_fZNear			(_fZNear)
	, m_fZFar			(_fZFar)
	, m_fFOV			(_fFOV)
	, m_fAspectRatio	(_fAspectRatio)
	, m_pObject3D		(_pObject)
{

}

//-------------------------------------------------
CCamera::CCamera()
	: m_pObject3D		(NULL)
	, m_fFOV			(Math::Angles::Deg2Rad(60.f))
	, m_fAspectRatio	(16.f/9.f)
	, m_fZNear			(0.1f)				
	, m_fZFar			(100.f)	
{

}

//-------------------------------------------------
CCamera::~CCamera()
{
	m_pObject3D = NULL;
}

//-------------------------------------------------
void CCamera::UpdateMatrices()
{
	CreateViewMatrix();
	CreateProjectionMatrix();
}

//-------------------------------------------------
void CCamera::CreateViewMatrix()
{
	Vect3f eye		= GetEye();
	Vect3f look_at	= GetLookAt();
	Vect3f vec_up	= GetVecUp();

	XMVECTOR l_Eye = XMVectorSet( eye.x, eye.y, eye.z, 0.0f );
	XMVECTOR l_LookAt = XMVectorSet( look_at.x, look_at.y, look_at.z, 0.0f );
	XMVECTOR l_VecUp = XMVectorSet( vec_up.x, vec_up.y, vec_up.z, 0.0f );

	XMMATRIX l_View = XMMatrixLookAtLH( l_Eye, l_LookAt, l_VecUp );
	m_ViewMatrix = Mat44f(l_View);
}

//-------------------------------------------------
void CCamera::CreateProjectionMatrix()
{
	XMMATRIX l_Projection = XMMatrixPerspectiveFovLH(m_fFOV, m_fAspectRatio, m_fZNear, m_fZFar);
	m_ProjectionMatrix = Mat44f(l_Projection);
}

